#include "threadpool.h"

void *func(void* arg){
    threadpool_t *threadpool=(threadpool_t *)arg;
    while(1){
        int netfd;
        while(threadpool->exitflag==0 && threadpool->taskArr->size==0){
            pthread_cond_wait(&threadpool->cond,&threadpool->mutex);
        }
        if(threadpool->exitflag){
            printf("%d号工人线程即将退出\n",getpid());
            pthread_mutex_unlock(&threadpool->mutex);
            pthread_exit(NULL);
        }
        printf("%d号线程正在开始执行任务\n",getpid());
        netfd=Quepop(threadpool->taskArr);
        pthread_mutex_unlock(&threadpool->mutex);

        //进行下载任务
        transFile(netfd);
        close(netfd); 
    }

    return NULL;
}

int makeworker(threadpool_t *threadpool){
    for(int i=0;i<threadpool->tidArr->workNum;i++){
        pthread_create(&threadpool->tidArr->arr[i],NULL,func,threadpool);
    }
    return 1;
}

